package com.jwgf.smarowindingmachine_unity.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.jwgf.common.ViewCache;
import com.jwgf.group.GroupMap;
import com.jwgf.protocolstack.ProtocolStack;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.CanCommand;
import com.jwgf.smarowindingmachine_unity.logic.FunctionSet;
import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;
import com.jwgf.smarowindingmachine_unity.logic.Report;
import com.jwgf.smarowindingmachine_unity.logic.SmaroWindingMachineCanReadSucceedListener;
import com.jwgf.smarowindingmachine_unity.ui.ObservableScrollView.OnScrollChangedListener;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;

public class EquipmentMaintenanceFunctionFragment extends BaseFragment implements OnScrollChangedListener, OnClickListener,
        OnEditorActionListener {
    public static final String fragmentTag = "EquipmentMaintenanceFunctionFragment";
    public static ProtocolStack canHandStack;
    private Button layoutSeperator1;
    private Button layoutSeperator2;
    private Button layoutSeperator3;
    private RelativeLayout layoutSeperator4;
    private float pageSeperator1;
    private float pageSeperator2;
    private float pageSeperator3;
    private float pageSeperator4;
    private Button btn_single_spindle;
    private Button btn_group;
    private Button btn_all_spindle;
    private Button btn_initial_spindle_state;
    private ImageButton btn_function_left_arrow;
    private ImageButton btn_function_right_arrow;
    private EditText function_text_value;
    private RelativeLayout function_title_layout;
    private int button_flag = 1;
    private int text_select = 1;
    private int total_spindles_number;
    private int total_groups_number;
    private TextView initial_spindle_failed;
    // 单锭
    private Button btn_yarn_length_reset;// 30DB.0
    private Button btn_lot_change_package_finish;// 30DB.1
    private Button btn_lot_change_cop_finish;// 30DB.2
    private Button btn_lot_change_lot_suspension;// 30DB.3
    private Button btn_forced_cop_change;// 30DB.4
    private Button btn_forced_splice;// 30DB.5
    private Button btn_cop_change_on;// 30DB.6
    private Button btn_cop_change_off;
    private Button btn_clearer_test_state_on;// 30DB.7
    private Button btn_clearer_test_state_off;// 30DB.8
    private Button btn_auto_running_on;// 30DB.11
    private Button btn_auto_running_off;// 30DB.12
    private Button btn_forced_tension_sample;// 30DB.15
    private Button btn_query_working_data;// 40
    private Button btn_spindle_initial_by_manual;
    private Button btn_air_door_on;
    private Button btn_air_door_off;
    // 分组
    private Button btn_group_yarn_length_reset;// 30DB.0
    private Button btn_group_lot_change_package_finish;// 30DB.1
    private Button btn_group_lot_change_cop_finish;// 30DB.2
    private Button btn_group_lot_change_lot_suspension;// 30DB.3
    private Button btn_group_forced_cop_change;// 30DB.4
    private Button btn_group_forced_splice;// 30DB.5
    private Button btn_group_cop_change_on;// 30DB.6
    private Button btn_group_cop_change_off;
    private Button btn_group_clearer_test_state_on;// 30DB.7
    private Button btn_group_clearer_test_state_off;// 30DB.8
    private Button btn_group_auto_running_on;// 30DB.11
    private Button btn_group_auto_running_off;// 30DB.12
    private Button btn_group_query_tension_sample;// 40
    private Button btn_group_clear_current_shift_groups_data;// 与报表有关
    private Button btn_group_initial_by_manual;
    private Button btn_air_door_group_on;
    private Button btn_air_door_group_off;
    // 全部
    private Button btn_all_yarn_length_reset;// 30DB.0
    private Button btn_all_lot_change_package_finish;// 30DB.1
    private Button btn_all_lot_change_cop_finish;// 30DB.2
    private Button btn_all_lot_change_lot_suspension;// 30DB.3
    private Button btn_all_forced_cop_change;// 30DB.4
    private Button btn_all_forced_splice;// 30DB.5
    private Button btn_all_cop_change_on;// 30DB.6
    private Button btn_all_cop_change_off;
    private Button btn_all_clearer_test_state_on;// 30DB.7
    private Button btn_all_clearer_test_state_off;// 30DB.8
    private Button btn_all_auto_running_on;// 30DB.11
    private Button btn_all_auto_running_off;// 30DB.12
    private Button btn_all_initial_by_manual;
    private Button btn_all_clear_current_shift_data;// 报表相关
    private Button btn_air_door_all_on;
    private Button btn_air_door_all_off;

    ObservableScrollView scrollView;
    private MachineParameterSharedPreference sp = MachineParameterSharedPreference.getInstance();

    private Disposable disposable;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = ViewCache.getInstance().getView(R.layout.fragment_equipment_maintenance_fuction);
        layoutSeperator1 = (Button) v.findViewById(R.id.btn_yarn_length_reset);
        layoutSeperator1.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                layoutSeperator1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                pageSeperator1 = layoutSeperator1.getY();

            }
        });
        layoutSeperator2 = (Button) v.findViewById(R.id.btn_group_yarn_length_reset);
        layoutSeperator2.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                layoutSeperator2.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                pageSeperator2 = layoutSeperator2.getY();

            }
        });
        layoutSeperator3 = (Button) v.findViewById(R.id.btn_all_yarn_length_reset);
        layoutSeperator3.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                layoutSeperator3.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                pageSeperator3 = layoutSeperator3.getY();

            }
        });
        layoutSeperator4 = (RelativeLayout) v.findViewById(R.id.initial_spindle_state);
        layoutSeperator4.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                layoutSeperator4.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                pageSeperator4 = layoutSeperator4.getY();

            }
        });
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        scrollView = (ObservableScrollView) getActivity().findViewById(R.id.function_parameter);
        scrollView.setScrollChangedListener(this);
        btn_single_spindle = (Button) getActivity().findViewById(R.id.btn_single_spindle);
        btn_group = (Button) getActivity().findViewById(R.id.btn_function_group);
        btn_all_spindle = (Button) getActivity().findViewById(R.id.btn_all_spindle);
        btn_initial_spindle_state = (Button) getActivity().findViewById(R.id.btn_initial_spindle_state);
        btn_single_spindle.setOnClickListener(this);
        btn_group.setOnClickListener(this);
        btn_all_spindle.setOnClickListener(this);
        btn_initial_spindle_state.setOnClickListener(this);
        btn_single_spindle.setActivated(true);

        total_spindles_number = Integer.valueOf(sp.getValue(0x29, 0x01));
        total_groups_number = GroupMap.getInstance().getGroupCount();
        function_title_layout = (RelativeLayout) getActivity().findViewById(R.id.function_title_layout);
        function_text_value = (EditText) getActivity().findViewById(R.id.function_text_value);
        function_text_value.setSelectAllOnFocus(true);
        function_text_value.setOnEditorActionListener(this);
        function_text_value.setBackgroundDrawable(null);
        function_text_value.setText("1");
        btn_function_left_arrow = (ImageButton) getActivity().findViewById(R.id.btn_function_left_arrow);
        btn_function_right_arrow = (ImageButton) getActivity().findViewById(R.id.btn_function_right_arrow);
        btn_function_left_arrow.setOnClickListener(this);
        btn_function_right_arrow.setOnClickListener(this);

        initial_spindle_failed = (TextView) getActivity().findViewById(R.id.initial_spindle_failed);
        // 单锭
        btn_yarn_length_reset = (Button) getActivity().findViewById(R.id.btn_yarn_length_reset);
        btn_lot_change_package_finish = (Button) getActivity().findViewById(R.id.btn_lot_change_package_finish);
        btn_lot_change_cop_finish = (Button) getActivity().findViewById(R.id.btn_lot_change_cop_finish);
        btn_lot_change_lot_suspension = (Button) getActivity().findViewById(R.id.btn_lot_change_lot_suspension);
        btn_forced_cop_change = (Button) getActivity().findViewById(R.id.btn_forced_cop_change);
        btn_forced_splice = (Button) getActivity().findViewById(R.id.btn_forced_splice);
        btn_cop_change_on = (Button) getActivity().findViewById(R.id.btn_cop_change_on);
        btn_cop_change_off = (Button) getActivity().findViewById(R.id.btn_cop_change_off);
        btn_clearer_test_state_on = (Button) getActivity().findViewById(R.id.btn_clearer_test_state_on);
        btn_clearer_test_state_off = (Button) getActivity().findViewById(R.id.btn_clearer_test_state_off);
        btn_auto_running_on = (Button) getActivity().findViewById(R.id.btn_auto_running_on);
        btn_auto_running_off = (Button) getActivity().findViewById(R.id.btn_auto_running_off);
        btn_query_working_data = (Button) getActivity().findViewById(R.id.btn_query_working_data);
        btn_forced_tension_sample = (Button) getActivity().findViewById(R.id.btn_forced_tension_sample);
        btn_spindle_initial_by_manual = (Button) getActivity().findViewById(R.id.btn_spindle_initial_by_manual);
        btn_air_door_on = getActivity().findViewById(R.id.btn_air_door_on);
        btn_air_door_off = getActivity().findViewById(R.id.btn_air_door_off);
        // 分组
        btn_group_yarn_length_reset = (Button) getActivity().findViewById(R.id.btn_group_yarn_length_reset);
        btn_group_lot_change_package_finish = (Button) getActivity()
                .findViewById(R.id.btn_group_lot_change_package_finish);
        btn_group_lot_change_cop_finish = (Button) getActivity().findViewById(R.id.btn_group_lot_change_cop_finish);
        btn_group_lot_change_lot_suspension = (Button) getActivity()
                .findViewById(R.id.btn_group_lot_change_lot_suspension);
        btn_group_forced_cop_change = (Button) getActivity().findViewById(R.id.btn_group_forced_cop_change);
        btn_group_forced_splice = (Button) getActivity().findViewById(R.id.btn_group_forced_splice);
        btn_group_cop_change_on = (Button) getActivity().findViewById(R.id.btn_group_cop_change_on);
        btn_group_cop_change_off = (Button) getActivity().findViewById(R.id.btn_group_cop_change_off);
        btn_group_clearer_test_state_on = (Button) getActivity().findViewById(R.id.btn_group_clearer_test_state_on);
        btn_group_clearer_test_state_off = (Button) getActivity().findViewById(R.id.btn_group_clearer_test_state_off);
        btn_group_auto_running_on = (Button) getActivity().findViewById(R.id.btn_group_auto_running_on);
        btn_group_auto_running_off = (Button) getActivity().findViewById(R.id.btn_group_auto_running_off);
        btn_group_query_tension_sample = (Button) getActivity().findViewById(R.id.btn_group_query_tension_sample);
        btn_group_clear_current_shift_groups_data = (Button) getActivity()
                .findViewById(R.id.btn_group_clear_current_shift_groups_data);
        btn_group_initial_by_manual = (Button) getActivity().findViewById(R.id.btn_group_initial_by_manual);
        btn_air_door_group_on = getActivity().findViewById(R.id.btn_air_door_group_on);
        btn_air_door_group_off = getActivity().findViewById(R.id.btn_air_door_group_off);
        // 全部
        btn_all_yarn_length_reset = (Button) getActivity().findViewById(R.id.btn_all_yarn_length_reset);
        btn_all_lot_change_package_finish = (Button) getActivity().findViewById(R.id.btn_all_lot_change_package_finish);
        btn_all_lot_change_cop_finish = (Button) getActivity().findViewById(R.id.btn_all_lot_change_cop_finish);
        btn_all_lot_change_lot_suspension = (Button) getActivity().findViewById(R.id.btn_all_lot_change_lot_suspension);
        btn_all_forced_cop_change = (Button) getActivity().findViewById(R.id.btn_all_forced_cop_change);
        btn_all_forced_splice = (Button) getActivity().findViewById(R.id.btn_all_forced_splice);
        btn_all_cop_change_on = (Button) getActivity().findViewById(R.id.btn_all_cop_change_on);
        btn_all_cop_change_off = (Button) getActivity().findViewById(R.id.btn_all_cop_change_off);
        btn_all_clearer_test_state_on = (Button) getActivity().findViewById(R.id.btn_all_clearer_test_state_on);
        btn_all_clearer_test_state_off = (Button) getActivity().findViewById(R.id.btn_all_clearer_test_state_off);
        btn_all_auto_running_on = (Button) getActivity().findViewById(R.id.btn_all_auto_running_on);
        btn_all_auto_running_off = (Button) getActivity().findViewById(R.id.btn_all_auto_running_off);
        btn_all_initial_by_manual = (Button) getActivity().findViewById(R.id.btn_all_initial_by_manual);
        btn_all_clear_current_shift_data = (Button) getActivity().findViewById(R.id.btn_all_clear_current_shift_data);
        btn_air_door_all_on = getActivity().findViewById(R.id.btn_air_door_all_on);
        btn_air_door_all_off = getActivity().findViewById(R.id.btn_air_door_all_off);
        // 单锭
        btn_yarn_length_reset.setOnClickListener(this);
        btn_lot_change_package_finish.setOnClickListener(this);
        btn_lot_change_cop_finish.setOnClickListener(this);
        btn_lot_change_lot_suspension.setOnClickListener(this);
        btn_forced_cop_change.setOnClickListener(this);
        btn_forced_splice.setOnClickListener(this);
        btn_cop_change_on.setOnClickListener(this);
        btn_cop_change_off.setOnClickListener(this);
        btn_clearer_test_state_on.setOnClickListener(this);
        btn_clearer_test_state_off.setOnClickListener(this);
        btn_auto_running_on.setOnClickListener(this);
        btn_auto_running_off.setOnClickListener(this);
        btn_query_working_data.setOnClickListener(this);
        btn_forced_tension_sample.setOnClickListener(this);
        btn_spindle_initial_by_manual.setOnClickListener(this);
        btn_air_door_on.setOnClickListener(this);
        btn_air_door_off.setOnClickListener(this);
        // 分组
        btn_group_yarn_length_reset.setOnClickListener(this);
        btn_group_lot_change_package_finish.setOnClickListener(this);
        btn_group_lot_change_cop_finish.setOnClickListener(this);
        btn_group_lot_change_lot_suspension.setOnClickListener(this);
        btn_group_forced_cop_change.setOnClickListener(this);
        btn_group_forced_splice.setOnClickListener(this);
        btn_group_cop_change_on.setOnClickListener(this);
        btn_group_cop_change_off.setOnClickListener(this);
        btn_group_clearer_test_state_on.setOnClickListener(this);
        btn_group_clearer_test_state_off.setOnClickListener(this);
        btn_group_auto_running_on.setOnClickListener(this);
        btn_group_auto_running_off.setOnClickListener(this);
        btn_group_query_tension_sample.setOnClickListener(this);
        btn_group_clear_current_shift_groups_data.setOnClickListener(this);
        btn_group_initial_by_manual.setOnClickListener(this);
        btn_air_door_group_on.setOnClickListener(this);
        btn_air_door_group_off.setOnClickListener(this);
        // 全部
        btn_all_yarn_length_reset.setOnClickListener(this);
        btn_all_lot_change_package_finish.setOnClickListener(this);
        btn_all_lot_change_cop_finish.setOnClickListener(this);
        btn_all_lot_change_lot_suspension.setOnClickListener(this);
        btn_all_forced_cop_change.setOnClickListener(this);
        btn_all_forced_splice.setOnClickListener(this);
        btn_all_cop_change_on.setOnClickListener(this);
        btn_all_cop_change_off.setOnClickListener(this);
        btn_all_clearer_test_state_on.setOnClickListener(this);
        btn_all_clearer_test_state_off.setOnClickListener(this);
        btn_all_auto_running_on.setOnClickListener(this);
        btn_all_auto_running_off.setOnClickListener(this);
        btn_all_initial_by_manual.setOnClickListener(this);
        btn_all_clear_current_shift_data.setOnClickListener(this);
        btn_air_door_all_on.setOnClickListener(this);
        btn_air_door_all_off.setOnClickListener(this);

        disposable = Observable.interval(3, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Throwable {
                        String spindleStrTemp = "";
                        String spindleStr = "";
                        HashSet<Integer> set = (HashSet<Integer>) ProtocolStack.spindleNum.clone();
                        for (int i : set) {
                            spindleStrTemp = (i - 1) + "  ";
                            spindleStr += spindleStrTemp;
                        }
                        initial_spindle_failed.setText(spindleStr);
                    }
                });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_single_spindle:
                scrollView.smoothScrollTo(0, 0);
                function_title_layout.setVisibility(View.VISIBLE);
                button_flag = 1;
                text_select = 1;
                function_text_value.setText(String.valueOf(text_select));
                break;
            case R.id.btn_function_group:
                scrollView.smoothScrollTo(0, (int) pageSeperator2);
                function_title_layout.setVisibility(View.VISIBLE);
                button_flag = 2;
                text_select = 1;
                function_text_value.setText(String.valueOf(text_select));
                break;
            case R.id.btn_all_spindle:
                scrollView.smoothScrollTo(0, (int) pageSeperator3);
                function_title_layout.setVisibility(View.INVISIBLE);
                button_flag = 3;
                break;
            case R.id.btn_initial_spindle_state:
                scrollView.smoothScrollTo(0, (int) pageSeperator4);
                function_title_layout.setVisibility(View.INVISIBLE);
                button_flag = 4;
                break;
            case R.id.btn_function_right_arrow:
                if (button_flag == 1) {
                    if ((text_select > 0) && (text_select < total_spindles_number)) {
                        text_select++;
                        function_text_value.setText(String.valueOf(text_select));
                    }
                } else if (button_flag == 2) {
                    if ((text_select > 0) && (text_select < total_groups_number)) {
                        text_select++;
                        function_text_value.setText(String.valueOf(text_select));
                    }
                }
                break;
            case R.id.btn_function_left_arrow:
                if (button_flag == 1) {
                    if ((text_select > 1) && (text_select <= total_spindles_number)) {
                        text_select--;
                        function_text_value.setText(String.valueOf(text_select));
                    }
                } else if (button_flag == 2) {
                    if ((text_select > 1) && (text_select <= total_groups_number)) {
                        text_select--;
                        function_text_value.setText(String.valueOf(text_select));
                    }
                }
                break;
            case R.id.btn_yarn_length_reset:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createSpindleCmdFunction(text_select, 0x30, 0x1, 0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_lot_change_package_finish:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createSpindleCmdFunction(text_select, 0x30, 0x2, 0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_lot_change_cop_finish:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createSpindleCmdFunction(text_select, 0x30, 0x4, 0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_lot_change_lot_suspension:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createSpindleCmdFunction(text_select, 0x30, 0x8, 0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_forced_cop_change:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createSpindleCmdFunction(text_select, 0x30, 0x10, 0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_forced_splice:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createSpindleCmdFunction(text_select, 0x30, 0x20, 0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_cop_change_on:
                btn_cop_change_on.setSelected(true);
                btn_cop_change_off.setSelected(false);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createSpindleCmdFunction(text_select, 0x30, 0x0, 0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_cop_change_off:
                btn_cop_change_on.setSelected(false);
                btn_cop_change_off.setSelected(true);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createSpindleCmdFunction(text_select, 0x30, 0x40, 0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_clearer_test_state_on:
                btn_clearer_test_state_on.setSelected(true);
                btn_clearer_test_state_off.setSelected(false);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createSpindleCmdFunction(text_select, 0x30, 0x80, 0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_clearer_test_state_off:
                btn_clearer_test_state_on.setSelected(false);
                btn_clearer_test_state_off.setSelected(true);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createSpindleCmdFunction(text_select, 0x30, 0x100, 0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_auto_running_on:
                btn_auto_running_on.setSelected(true);
                btn_auto_running_off.setSelected(false);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createSpindleCmdFunction(text_select, 0x30, 0x800, 0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_auto_running_off:
                btn_auto_running_on.setSelected(false);
                btn_auto_running_off.setSelected(true);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createSpindleCmdFunction(text_select, 0x30, 0x1000, 0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_forced_tension_sample:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createSpindleCmdFunction(text_select, 0x30, 0x8000, 0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_query_working_data:
                SmaroWindingMachineCanReadSucceedListener.getInstance().clearQueryData();
                querySpindleData();
                new Handler(Looper.getMainLooper()).postDelayed((new Runnable() {

                    @Override
                    public void run() {
                        Bundle bd = new Bundle();
                        bd.putInt(QueryWorkingDataDialog.SPINDLE_INDEX, text_select);
                        QueryWorkingDataDialog dlg = new QueryWorkingDataDialog();
                        dlg.setArguments(bd);
                        dlg.show(getFragmentManager(), "query");
                    }
                }), 3500);
                break;
            case R.id.btn_air_door_on:
                btn_air_door_on.setSelected(true);
                btn_air_door_off.setSelected(false);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putPriorityCmd(CanCommand.createSpindleCmdFunction(text_select, 0x38, 1, 0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_air_door_off:
                btn_air_door_on.setSelected(false);
                btn_air_door_off.setSelected(true);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putPriorityCmd(CanCommand.createSpindleCmdFunction(text_select, 0x38, 0, 0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_group_yarn_length_reset:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createGroupCmdFunction(text_select, 0x30, 0x1));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_group_lot_change_package_finish:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createGroupCmdFunction(text_select, 0x30, 0x2));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_group_lot_change_cop_finish:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createGroupCmdFunction(text_select, 0x30, 0x4));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_group_lot_change_lot_suspension:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createGroupCmdFunction(text_select, 0x30, 0x8));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_group_forced_cop_change:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createGroupCmdFunction(text_select, 0x30, 0x10));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_group_forced_splice:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createGroupCmdFunction(text_select, 0x30, 0x20));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_group_cop_change_on:
                btn_group_cop_change_on.setSelected(true);
                btn_group_cop_change_off.setSelected(false);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createGroupCmdFunction(text_select, 0x30, 0x0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_group_cop_change_off:
                btn_group_cop_change_on.setSelected(false);
                btn_group_cop_change_off.setSelected(true);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createGroupCmdFunction(text_select, 0x30, 0x40));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_group_clearer_test_state_on:
                btn_group_clearer_test_state_on.setSelected(true);
                btn_group_clearer_test_state_off.setSelected(false);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createGroupCmdFunction(text_select, 0x30, 0x80));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_group_clearer_test_state_off:
                btn_group_clearer_test_state_on.setSelected(false);
                btn_group_clearer_test_state_off.setSelected(true);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createGroupCmdFunction(text_select, 0x30, 0x100));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_group_auto_running_on:
                btn_group_auto_running_on.setSelected(true);
                btn_group_auto_running_off.setSelected(false);
                FunctionSet.getInstance().setAllSpindleAutoRunningOn();
                break;
            case R.id.btn_group_auto_running_off:
                btn_group_auto_running_on.setSelected(false);
                btn_group_auto_running_off.setSelected(true);
                FunctionSet.getInstance().setAllSpindleAutoRunningOff();
                break;
            case R.id.btn_group_query_tension_sample:
                TensionSampleDialog dlg = new TensionSampleDialog();
                Bundle bd = new Bundle();
                bd.putInt(TensionSampleDialog.GROUP, text_select);
                dlg.setArguments(bd);
                dlg.show(getFragmentManager(), "tension");
//                TubeLengthSampleDialog dlg = new TubeLengthSampleDialog();
//                Bundle bd = new Bundle();
//                bd.putInt(TubeLengthSampleDialog.GROUP, text_select);
//                dlg.setArguments(bd);
//                dlg.show(getFragmentManager(), "tension");
                break;
            case R.id.btn_group_clear_current_shift_groups_data:
                // 报表相关
                Report.getInstance().clearGroupReportManual(text_select);
                break;
            case R.id.btn_air_door_group_on:
                btn_air_door_group_on.setSelected(true);
                btn_air_door_group_off.setSelected(false);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putPriorityCmd(CanCommand.createGroupCmdFunction(text_select, 0x38, 0x1));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_air_door_group_off:
                btn_air_door_group_on.setSelected(false);
                btn_air_door_group_off.setSelected(true);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createGroupCmdFunction(text_select, 0x38, 0x0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_all_yarn_length_reset:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createAllCmdFunction(0x30, 0x1));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_all_lot_change_package_finish:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createAllCmdFunction(0x30, 0x2));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_all_lot_change_cop_finish:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createAllCmdFunction(0x30, 0x4));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_all_lot_change_lot_suspension:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createAllCmdFunction(0x30, 0x8));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_all_forced_cop_change:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createAllCmdFunction(0x30, 0x10));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_all_forced_splice:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createAllCmdFunction(0x30, 0x20));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_all_cop_change_on:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createAllCmdFunction(0x30, 0x0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_all_cop_change_off:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createAllCmdFunction(0x30, 0x40));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_all_clearer_test_state_on:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createAllCmdFunction(0x30, 0x80));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_all_clearer_test_state_off:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putNormalCmd(CanCommand.createAllCmdFunction(0x30, 0x100));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_all_auto_running_on:
                btn_all_auto_running_on.setSelected(true);
                btn_all_auto_running_off.setSelected(false);
                FunctionSet.getInstance().setAllSpindleAutoRunningOn();
                break;
            case R.id.btn_all_auto_running_off:
                btn_all_auto_running_on.setSelected(false);
                btn_all_auto_running_off.setSelected(true);
                FunctionSet.getInstance().setAllSpindleAutoRunningOff();
                break;
            case R.id.btn_all_initial_by_manual:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.intAllGroup());
                WaitProgressDialog.show(getActivity());
                break;
            case R.id.btn_spindle_initial_by_manual:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.initSpindle(text_select));
                WaitProgressDialog.show(getActivity());
                break;
            case R.id.btn_group_initial_by_manual:
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.initGroup(text_select));
                WaitProgressDialog.show(getActivity());
                break;
            case R.id.btn_all_clear_current_shift_data:
                // 报表相关
                Report.getInstance().clearAllReportManual();
                break;
            case R.id.btn_air_door_all_on:
                btn_air_door_all_on.setSelected(true);
                btn_air_door_all_off.setSelected(false);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putPriorityCmd(CanCommand.createAllCmdFunction(0x38, 0x1));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            case R.id.btn_air_door_all_off:
                btn_air_door_all_on.setSelected(false);
                btn_air_door_all_off.setSelected(true);
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
                SmaroWindingMachineActivity.canHandStack
                        .putPriorityCmd(CanCommand.createAllCmdFunction(0x38, 0x0));
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
                break;
            default:
                break;
        }
    }

    private void querySpindleData() {
        ArrayList<byte[]> cmdList = new ArrayList<byte[]>();
        cmdList.add(CanCommand.getBus());
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x20, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x21, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x22, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x23, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x24, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x25, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x26, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x27, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x28, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x29, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x2A, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x30, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x31, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x32, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x33, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x2C, 0));
        cmdList.add(CanCommand.createSpindleCmdFunction(text_select, 0x40, 0x10, 0));
        cmdList.add(CanCommand.releaseBus());
        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);

    }

    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (y < pageSeperator2) {
            onSetButtonTrue(btn_single_spindle);
            function_title_layout.setVisibility(View.VISIBLE);
            button_flag = 1;
            text_select = 1;
            function_text_value.setText(String.valueOf(text_select));
        } else if (y < pageSeperator3 && y >= pageSeperator2) {
            onSetButtonTrue(btn_group);
            function_title_layout.setVisibility(View.VISIBLE);
            button_flag = 2;
            text_select = 1;
            function_text_value.setText(String.valueOf(text_select));
        } else if (y < pageSeperator4 && y >= pageSeperator3) {
            onSetButtonTrue(btn_all_spindle);
            function_title_layout.setVisibility(View.INVISIBLE);
            button_flag = 3;
        } else if (y >= pageSeperator4) {
            onSetButtonTrue(btn_initial_spindle_state);
            function_title_layout.setVisibility(View.INVISIBLE);
            button_flag = 4;
        }
    }

    private void onSetButtonTrue(Button button) {
        btn_single_spindle.setActivated(false);
        btn_group.setActivated(false);
        btn_all_spindle.setActivated(false);
        btn_initial_spindle_state.setActivated(false);
        button.setActivated(true);
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            disposable.dispose();
        } else {
            text_select = 1;
            function_text_value.setText(String.valueOf(text_select));
            total_groups_number = GroupMap.getInstance().getGroupCount();
            total_spindles_number = Integer.valueOf(sp.getValue(0x29, 0x01));
            disposable = Observable.interval(3, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<Long>() {
                        @Override
                        public void accept(Long aLong) throws Throwable {
                            String spindleStrTemp = "";
                            String spindleStr = "";
                            HashSet<Integer> set = (HashSet<Integer>) ProtocolStack.spindleNum.clone();
                            for (int i : set) {
                                spindleStrTemp = (i - 1) + "  ";
                                spindleStr += spindleStrTemp;
                            }
                            initial_spindle_failed.setText(spindleStr);
                        }
                    });
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            String strValue = v.getText().toString();
            if (TextUtils.isEmpty(strValue)) {
                function_text_value.setText(String.valueOf(text_select));
                return false;
            }
            switch (v.getId()) {
                case R.id.function_text_value:
                    if (button_flag == 1) {
                        if (Integer.parseInt(strValue) <= total_spindles_number) {
                            function_text_value.setText(strValue);
                            text_select = Integer.valueOf(strValue);
                        } else {
                            function_text_value.setText(String.valueOf(text_select));
                        }
                    } else if (button_flag == 2) {
                        if (Integer.parseInt(strValue) <= total_groups_number) {
                            function_text_value.setText(strValue);
                            text_select = Integer.valueOf(strValue);
                        } else {
                            function_text_value.setText(String.valueOf(text_select));
                        }
                    }
                    break;
            }
        }
        return false;
    }
}
